import { _decorator, Node, EventTouch, Vec2, Animation } from 'cc';
import { Bullet } from './Bullet';
import { BulletConfig } from './BulletConfig';
import { Enemy } from './Enemy';
import { EnemyConfig } from './EnemyConfig';
import { EventManager } from './EventManager';
import { EventType } from './EventType';
import { GameManager } from './GameManager';
import { JoyStick } from './JoyStick';
import { RollBase } from './RollBase';
import { UIConfig } from './UI/UIConfig';
import { UIManager } from './UIManager';
const { ccclass, property } = _decorator;

@ccclass('PlayerController')
export class PlayerController extends RollBase
{
    public Animator:Animation = null
    private animValueIsMoving = false
    private animatorDirty = false
    
    public AttackInterval:number = 1 // 攻速
    public AttackTimer:number = 0

    onLoad()
    {
        super.onLoad()
        this.MaxHP = GameManager.instance.PlayerMaxHP
        this.CurHP = GameManager.instance.PlayerHP
        //this.MoveSpeed = 5 //暂时没用
        this.Animator = this.getComponent(Animation)
    }

    start()
    {
        // input.on(Input.EventType.TOUCH_START, this.OnTouchStart, this)

        EventManager.instance.DispatchEvent(EventType.HPChange, this.CurHP, 0,this.MaxHP)
    }

    update(deltaTime)
    {
        if (GameManager.instance.IsGamePause > 0) {
            return
        }
        if (this.AttackTimer > 0) {
            this.AttackTimer -= deltaTime
        }
        else
        {
            this.AttackTimer = this.AttackInterval
            this.Attack()
        }

        if (this.animatorDirty) {
            this.UpdateAnimator()
        }
    }

    Attack()
    {
        // todo 子弹需要设置存活时间自动销毁
        let e = GameManager.instance.GetClosestEnemy()
        if (e == null) { // todo 即使没有敌人也会进攻击CD，待优化
            return
        }
        let curPos = this.node.getPosition()
        let ePos = e.getPosition()
        let dir = new Vec2(ePos.x - curPos.x, ePos.y - curPos.y)
        let config = new BulletConfig()
        config.nMoveSpeed = 5
        config.v2Dir = dir.normalize()
        config.v2InitPos = new Vec2(curPos.x, curPos.y)
        config.Source = this.node

        UIManager.instance.CreateBullet(config)
    }

    OnHurt(dmg: number, source: Node)
    {
        source.getComponent(Enemy).Kill()
        EventManager.instance.DispatchEvent(EventType.HPChange, this.CurHP, dmg,this.MaxHP)
    }

    OnKill(source: Node)
    {
        if (this.CurHP <= 0)
        {
            UIManager.instance.OpenUI(UIConfig.GameOver)
        }
    }

    // 玩家死亡比较特殊，特殊处理一下
    Kill(source: Node = null)
    {
        this.OnKill(source)
    }

    OnTouchStart(event: EventTouch)
    {
        // let curPos = this.node.getPosition()
        // let touchPos = UIManager.instance.TouchPosToUIPos(event.getLocation())
        // let dir = new Vec2(touchPos.x - curPos.x, touchPos.y - curPos.y)
        // let config = new BulletConfig()
        // config.nMoveSpeed = 5
        // config.v2Dir = dir.normalize()
        // config.v2InitPos = new Vec2(curPos.x, curPos.y)
        // config.Source = this.node

        // UIManager.instance.CreateBullet(config)
    }

    //加血
    AddHP(num:number)
    {
        this.CurHP += num
        this.CurHP = Math.min(this.CurHP, this.MaxHP)
        EventManager.instance.DispatchEvent(EventType.HPChange, this.CurHP, -num,this.MaxHP)//有点抽象，后面改一下
    }

    AddAttackInterval(num:number)
    {
        this.AttackInterval += num
        this.AttackInterval = Math.max(this.AttackInterval, 0.1) // 攻速最低不能低于0.1
    }

    IncreaseHPMax(num:number)
    {
        this.MaxHP += num
        this.CurHP += num
        EventManager.instance.DispatchEvent(EventType.HPChange, this.CurHP, -num,this.MaxHP)//有点抽象，后面改一下
    }

    // 动画状态机
    UpdateAnimator()
    {
        if (this.animValueIsMoving) {
            this.Animator.crossFade('Player_Run', 0.3)
        }
        else
        {
            this.Animator.crossFade('Player_Idle', 0.3)
        }
    }

    SetIsMoving(flag)
    {
        if (this.animValueIsMoving != flag) {
            this.animValueIsMoving = flag
            this.animatorDirty = true
        }
    }
}


